<?php

class InstallController extends Controller {

	private $db;

	public function __construct() {
		parent::__construct(__CLASS__);
	}

	public function indexAction() {
		$this->rend('index');
	}

	public function databaseAction($segments) {
		$this->db = DB::create($segments[0]);

		switch ($segments[0]) {
			case 'reading':
				$password = sha1('icui4cu');
				$nancyPassword = sha1('16001');
				$sql = "CREATE TABLE book (
							id INTEGER PRIMARY KEY,
							cover TEXT,
							name TEXT,
							author TEXT,
							page INTEGER,
							created_at NUMERIC
						);
						CREATE TABLE plan (
							id INTEGER PRIMARY KEY,
							name TEXT,
							book INTEGER,
							start NUMERIC,
							end NUMERIC,
							created_at NUMERIC
						);
						CREATE TABLE note (
							id INTEGER PRIMARY KEY,
							content TEXT,
							plan INTEGER,
							created_at NUMERIC
						);
						CREATE TABLE ftp_server (
							id INTEGER PRIMARY KEY,
							name TEXT,
							host TEXT,
							port INTEGER,
							username TEXT,
							password TEXT,
							created_at NUMERIC
						);
						CREATE TABLE ftp_project (
							id INTEGER PRIMARY KEY,
							name TEXT,
							local_dir TEXT,
							remote_dir TEXT,
							created_at NUMERIC
						);
						CREATE TABLE user (
							`id` INTEGER PRIMARY KEY,
							`ip` TEXT,
							`name` TEXT,
							`nick` TEXT,
							`email` TEXT,
							`password` TEXT,
							`created_at` NUMERIC
						);
						CREATE TABLE permission (
							`id` INTEGER PRIMARY KEY,
							`user_id` TEXT,
							`permission` TEXT,
							`created_at` NUMERIC
						);
						CREATE TABLE chat (
							`id` INTEGER PRIMARY KEY,
							`from` INTERGER,
							`to` INTERGER,
							`content` TEXT,
							`ip` TEXT,
							`created_at` NUMERIC
						);
						INSERT INTO `user` (
							`ip`,
							`name`,
							`nick`,
							`email`,
							`password`,
							`created_at`
						) VALUES (
							'0.0.0.0',
							'古道倦马',
							'古道倦马',
							'gudaojuanma@gmail.com',
							'{$password}',
							NOW()
						);
						INSERT INTO `user` (
							`ip`,
							`name`,
							`nick`,
							`email`,
							`password`,
							`created_at`
						) VALUES (
							'0.0.0.0',
							'Kitty',
							'Nancy',
							'705287119@qq.com',
							'{$nancyPassword}',
							NOW()
						);	";
				break;
			case 'chat':
				$sql = ";";
				break;
		}

		if ($this->db->exec($sql)) {
			setFlash('success', "create db({$segments[0]}) successful!");
		} else {
			setFlash('error', "{$sql}\nCreate db({$segments[0]}) failed!");
		}
		redirect(url('install/index'));
	}

}